package com.luzhikun.path.com.luzhikun.myExamSystem.controller; /**
 * ClassName: ${NAME}
 * Description: ${Description}
 * date: 2021/4/23 14:49
 *
 * @author luzhikun
 * @version
 * @since JDK 1.8
 */

import com.luzhikun.path.com.luzhikun.myExamSystem.dao.UserDao;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.IOException;
import java.io.PrintWriter;

public class UserLoginServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String userName, password;
        //创建数据库连接对象
        UserDao userDao = new UserDao();
//      boolean login;
        PrintWriter out = null;
        boolean isFlag;
        //1.调用请求对象对请求体使用UTF-8字符集进行编辑
        request.setCharacterEncoding("UTF-8");
        //2.调用请求对象读取请求体参数信息
        userName = request.getParameter("userName");
        password = request.getParameter("password");
        System.out.println(userName);
        System.out.println(password);
        //3.调用Dao将查询验证信息推送到数据库服务器上
        isFlag = userDao.login(userName, password);
        System.out.println(isFlag);
        //4.调用响应对象，根据验证对象将不同资源文件写入到响应头中，交给浏览器
        out = response.getWriter();
        if (isFlag == true) {
            response.sendRedirect("/myWeb/index.html");
        } else {
            response.sendRedirect("/myWeb/loginError.html");
        }
    }
}
